<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<div class="window-cover"></div>
<div class="goTop">
    <div class="-icon -top"></div>
</div>
<script>
    function showCover(index) {
        const tar = window.covers[index] || null
        tar.classList.add('--cover')
        tar.id = "--cover"
        tar.onclick = (e) => {
            document.querySelector('.window-cover').classList.remove('active')
        }
        let old = document.querySelector('#--cover')
        if (old) old.remove()
        let cv = document.querySelector('.window-cover')
        cv.appendChild(tar)
        cv.onclick = (e) => {
            e.target.classList.remove('active')
        }
        cv.classList.add('active')
    }
    (($) => {
        $.toggleActive = (eleQuery) => {
            const target = document.querySelectorAll(eleQuery)
            for (let i = 0; i < target.length; i++) {
                if (target[i].classList.contains('active')) {
                    target[i].classList.remove('active')
                } else {
                    target[i].classList.add('active')
                }
            }
        }

        let scrollDiv = document.querySelectorAll('.scroller-y')
        let tar = document.querySelector('.goTop')
        if (!scrollDiv || !tar) return
        scrollDiv = scrollDiv[scrollDiv.length - 1]
        scrollDiv.addEventListener('scroll', () => {
            let t = scrollDiv.scrollTop;
            if (tar.classList.contains('active') || t < 100) {
                tar.classList.remove('active')
            }
            if (t > 99) {
                tar.classList.add('active')
            }
        })

        tar.addEventListener('click', () => {
            scrollDiv.scrollTo({
                top: 0,
                behavior: "smooth"
            })
        })

        exImgGroup()

        // 文章中的相册解析
        function exImgGroup() {
            const root = document.querySelector('.---img-group')
            if (!root) return
            const list = document.querySelectorAll('.---img-group > img')
            let html = '<div class="---list">'
            window.covers = []
            for (let i = 0; i < list.length; i++) {
                let img = new Image()
                img.src = list[i].src
                img.alt = list[i].alt
                img.title = list[i].alt
                window.covers.push(img)
                html += `<div class="bg-cover" onclick="showCover(${i})" style="background-image:url('${list[i].src}');">
                <div class="cover-title text-ellipsis">${list[i].alt||'无题'}</div>
                </div>`
            }
            html += '</div>'
            root.innerHTML = html
        }


        window.$ = $
    })({})
</script>